package com.yuexunit.finance.system.action.v1;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yuexunit.finance.common.httpclient.HttpClientHelper;
import com.yuexunit.finance.member.entity.EnterpriseEntity;
import com.yuexunit.finance.member.service.EnterpriseService;
import com.yuexunit.finance.system.action.v1.model.LocationInfo;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.jsonutil.JSON;
import com.yuexunit.persist.annotation.Transactional;

/**
*@version 创建时间：2017年10月19日 下午7:20:45
*@author wenwenlong
*类说明
*/

@Implements(BaiDuLocationActionApi.class)
public class BaiDuLocationActionApiImpl implements BaiDuLocationActionApi {
	
	private EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);

	@Override
	public LocationInfo getLocationInfo(String address) {
		Map<String, String> params = new HashMap<>();
		params.put("address", address);
		params.put("output", "json");
		params.put("ak", "P0dOGYGDORQ3H0GWZvPFszd9");
		
		String responseText = HttpClientHelper.post("http://api.map.baidu.com/geocoder/v2/", params);
		System.out.println(responseText);
		LocationInfo result = JSON.toBean(responseText, LocationInfo.class);
		return result;
	}

	@Override
	@Transactional
	public void initLocationInfo() {
		List<EnterpriseEntity> ees = enterpriseService.inquireAllEnterpriseList();
		for (EnterpriseEntity ee : ees) {
			LocationInfo li = getLocationInfo(ee.getProvince() + ee.getCity() + ee.getDistrict());
			if (0 == li.getStatus()) {
				ee.setLat(li.getResult().getLocation().getLat() + "");
				ee.setLng(li.getResult().getLocation().getLng() + "");
				enterpriseService.persistEnterprise(ee);
			}
		}
	}
}
